/**
 * 复杂数组去重方法
 * @param array
 * @param key
 * @returns
 */
export function uniqueArr2(array: Recordable[], key: string) {
  return Array.from(array.reduce((m, n) => m.set(n[key], n), new Map()).values());
}

/**
 * 简单数组去重
 * @param array
 * @returns
 */
export function uniqueArr(array: string[]) {
  return Array.from(new Set(array));
}

export function omit<T extends object, U extends keyof T>(obj: T, keys: ReadonlyArray<U>) {
  const clone: T = { ...obj };

  keys.forEach((key) => {
    if (key in clone) {
      delete clone[key];
    }
  });
  return clone;
}

export function transformArray(data: any[], format: (item: any) => Recordable) {
  return data.reduce((pre, item) => {
    const newItem = format(item);
    pre.push({
      ...newItem,
      ...(Array.isArray(item.children) && { children: transformArray(item.children, format) })
    });
    return pre;
  }, []);
}
